{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding: utf-8\n",
    "import pymongo,json\n",
    "import pandas as pd\n",
    "from pandas import Series,DataFrame\n",
    "import csv\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import time,datetime\n",
    "# from datetime import datetime\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "\n",
    "client = pymongo.MongoClient('localhost', 27017)\n",
    "futures = client.futures\n",
    "unit = futures.unit\n",
    "main = futures.main\n",
    "signal = futures.signal\n",
    "lots = futures.lots\n",
    "\n",
    "start='20110101'\n",
    "# var='A'\n",
    "\n",
    "unit = DataFrame(list(unit.find()))\n",
    "main= DataFrame(list(main.find({'date': {'$gte': start}})))\n",
    "lots = DataFrame(list(lots.find()))\n",
    "\n",
    "unit=unit[['variety','unit','minPrice']]\n",
    "lots = lots[['variety','lots']]\n",
    "# del main['_id']\n",
    "main['signal']=main.groupby('variety')['交易信号'].shift(1)\n",
    "main=main.dropna()\n",
    "del main['_id']\n",
    "# print(main.head())\n",
    "df=pd.merge(main,unit,on=['variety'])#.drop_duplicates()\n",
    "df1=pd.merge(df,lots,on=['variety'])#.drop_duplicates()\n",
    "df=df1\n",
    "# print(all_h1)\n",
    "df['盈亏计算']= df.apply(lambda x: x['change'] * x['signal'] * x['unit']*x['lots'],axis=1)#.drop_duplicates()\n",
    "# 交易成本\n",
    "df['交易成本'] = df.apply(lambda x: x['unit']*x['minPrice']*x['lots']*abs(x['signal']),axis=1)#.drop_duplicates()\n",
    "df['净利润'] = df['盈亏计算'] -df['交易成本']\n",
    "# print(all_h.head())\n",
    "df= df.drop_duplicates()\n",
    "df['pnl'] = df['净利润'].cumsum()\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "df.to_csv(r\"e:\\signaldate.csv\", mode='a', encoding='ANSI', header=True)\n",
    "# signal.insert(json.loads(df.T.to_json()).values())\n",
    "# print(json.loads(df.T.to_json()).values())\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1.0,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
